<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
    >
      <el-form-item :label="$t('parameterCode')" prop="code">
        <el-input
          v-model="queryParams.code"
          :placeholder="$t('inputParameterCode')"
          clearable
          size="small"
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('parameterContent')" prop="content">
        <el-input
          v-model="queryParams.content"
          :placeholder="$t('inputParameterContent')"
          clearable
          size="small"
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >{{ $t('search') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t('reset')
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" border :data="parameterList" @sort-change="sortChange">
      <el-table-column
        :label="$t('parameterCode')"
        sortable
        prop="code"
        min-width="200"
      />
      <el-table-column
        :label="$t('parameterContent')"
        prop="content"
        sortable
        :show-overflow-tooltip="true"
        min-width="200"
      />
      <el-table-column
        :label="$t('parameterValue')"
        prop="value"
        min-width="200"
      />
      <el-table-column
        :label="$t('operate')"
        align="center"
        width="250"
        class-name="fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
          >{{ $t('detailBtn') }}</el-button>
          <el-button
            v-auth="['UPDATE']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >{{ $t('modifyBtn') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList"
    />

    <ParameterInquire ref="parameterInquire" />
    <ParameterSheet ref="parameterSheet" @refreshList="getList" />
  </div>
</template>

<script>
import store from '@/store';
import { mapGetters } from 'vuex';
import ParameterInquire from '@/views/system/components/ParameterInquire';
import ParameterSheet from '@/views/system/components/ParameterSheet';
export default {
  name: 'Parameter',
  components: {
    ParameterInquire,
    ParameterSheet
  },
  data() {
    return {
      permissions: this.handleMenuPermission(this),
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        page: 1,
        page_size: 10,
        code: undefined,
        content: undefined,
        ordering: undefined
      }
    };
  },
  computed: {
    ...mapGetters(['parameterList'])
  },
  created() {
    // this.handleRbac();
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    async getList() {
      this.loading = true;
      var resp = await store.dispatch('system/getSystemParameterList', this.queryParams);
      if (resp.success) {
        this.total = resp.data.count;
        this.loading = false;
      } else {
        this.targaMessageBox(this, resp.msg);
        this.loading = false;
      }
    },
    // handleRbac() {
    //   this.permissions = this.handleMenuPermission(this);
    // },
    sortChange({ prop, order }) {
      var ordering;
      if (order === 'ascending') {
        ordering = '' + prop;
      } else if (order === 'descending') {
        ordering = '-' + prop;
      } else {
        ordering = undefined;
      }
      this.queryParams.ordering = ordering;

      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.handleQuery();
    },
    handleUpdate(row) {
      this.$refs.parameterSheet.openSheetDialog(this.handleObjectCopy(row));
    },
    handleDetail(row) {
      this.$refs.parameterInquire.openSheetDialog(this.handleObjectCopy(row));
    }
  }
};
</script>
